/**
 * Created by Administrator on 2015/6/22 0022.
 */
define('chat', ['avalon'], function () {
    return chat = avalon.define({
        $id: "chat",
        ready: function () {
            console.log("chat加载成功")
            avalon.scan()
//            chat.setListH()
//            setInterval(function () {
//                chat.Get(chat.Start, chat.Length)
//            }, 1000)


            //注册获取消息通知
            ws.addListen('gm',function(res){
                //获取数据
                console.log(res)
                var decoded=res[0].replace(/\[\|\]/g,",")
                var data = JSON.parse(decoded)//转换为json

                //数据格式转换;
                data.Content=data.msg;
                data.Name=data.un;
                data.Time=data.time*1000;
                chat.list.splice(0, 0, data)
                chat.duang()
            })


            chat.Get()


        },
        isFocus:false,
        isHover:false,
        onFocus:function(){
            chat.isFocus=true
        },
        outFocus:function(){
            if(chat.isHover){

            }
            else{

                chat.isFocus=false
                tEmoticon.isShow=false
            }
        },
        onHover:function(){
            chat.isHover=true
        },
        outHover:function(){
            chat.isHover=false
        },
        contant:"未连接",
        type:"webSocket",
        side:"./package/side/side.html",
        //设置消息高度
        listH: 300,
        setListH: function () {
            function findDimensions() //函数：获取尺寸
            {
//获取窗口宽度
                if (window.innerWidth)
                    winWidth = window.innerWidth;
                else if ((document.body) && (document.body.clientWidth))
                    winWidth = document.body.clientWidth;
//获取窗口高度
                if (window.innerHeight)
                    winHeight = window.innerHeight;
                else if ((document.body) && (document.body.clientHeight))
                    winHeight = document.body.clientHeight;
//通过深入Document内部对body进行检测，获取窗口大小
                if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
                    winHeight = document.documentElement.clientHeight;
                    winWidth = document.documentElement.clientWidth;
                }
//结果输出至两个文本框
                return {
                    h: winHeight,
                    w: winWidth
                }
            }

            chat.listH = findDimensions().h - 150;
        },


        //获取消息
        list: [],
        Start: 0,
        Length: 40,

        Get: function (Start, Length) {

            var data = {}
            if (Start > 0) {
                data.Start = Start
            }
            if (Length != "") {
                data.Length = Length
            }
            $.call({
                type: "post",
                i: "MCGetChat",
                data: data,
                success: function (res) {
                    if (res.c == 200) {
                        if (res.d != null && res.d.length > 0) {

//                            数组添加
                            var ready2push = res.d.reverse()
                            for (var i = 0; i < ready2push.length; i++) {
                                chat.list.splice(0, 0, ready2push[i])

                                //更新条目位置
                                var newStart = Number(res.d[i].ID)
                                chat.Start = chat.Start < newStart ? newStart : chat.Start;
                            }
                            chat.duang()

                            //滚动滚动条
//                            var em;
//                            if(document.documentElement&&document.documentElement.scrollTop){
//                                em=document.documentElement
//                            }
//                            else if(document.body){
//                                em=document.body
//                            }
//
//
//                            function sc(){
//                                var st=em.scrollTop
//                                em.scrollTop=st+5
//                                if(em.scrollTop>st){
//                                    setTimeout(function(){
//                                        sc()
//                                    },100/6)
//                                }
//                            }
//                            sc()


                        }
                    }
                }
            })
        },

        //播放声音
//        chatTip:"./src/audio/v1.ogg",
        audioN:1,
        duang:function(){
            var t=Math.floor(Math.random()*9+1); //1-9
            if(t===chat.audioN){
                //重新调用
                chat.duang()
            }
            else{
                var audioElement = document.createElement('audio');

                var url="./src/audio/v"+t+".ogg";
                audioElement.setAttribute('src', url);

                //播放声音
                audioElement.load;
                audioElement.play();
                chat.audioN=t


            }
        },

        GetHistory: function () {

        },
        //发送消息
        msg: "",
        lastMsg:[],
        index:0,
        send: function (msg) {
            if(chat.type=="ajax"){
                if (msg != "" ) {
                    $.call({
                        type: "post",
                        i: "MCSendChat",
                        data: {
                            msg: msg
                        },
                        success: function (res) {
                            if (res.c == 200) {
//                                chat.msg = '';
//                            chat.Get(chat.Start, chat.Length)
                            }
                        }
                    })
                    chat.lastMsg.push(msg)
                    chat.index++
                }
            }
            else if(chat.type=="webSocket"){
                if(msg==''){
                    tip.on("没有输入任何消息",0,3000)
                }
                else{
                    //新的调用
                    ws.call(1,"pm",{
                        msg:msg,
                        un:chat.user.UserName
                    })
                }





                chat.lastMsg.push(msg);
                chat.index++
            }
            chat.msg = '';
        },

        //快捷键
        hotKey: function (ev) {
            var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if (keyCode == 13) {
                chat.send(chat.msg);//处理函数
            }
            if (keyCode == 38) {

                chat.index=chat.index-1>=0?chat.index-1:0
                chat.msg=chat.lastMsg[chat.index];//处理函数
            }
            if (keyCode == 40) {
                chat.index=chat.index+1<chat.lastMsg.length-1?chat.index+1:chat.lastMsg.length-1
                chat.msg=chat.lastMsg[chat.index];//处理函数

            }
//                            alert(keyCode)
        },

        //弹出登录
        login:function(){
            modal.url='./plugins/stb/stb.html';
            modal.getIn();
        },
        inSide:false,
        user:{
            UserName:cache.go("UserName"),
            uid:cache.go("uid")
        },


        //登出
        logout:function(){
            tip.on('登出中...', 1);
            $.call({
                i: '6',
                type: 'post',
                dataType: 'json',
                success: function(data){
                    if(data.c == 200){
                        chat.inSide = false;
                        tip.off('登出中...', 1);
                        tip.on("退出成功", 1, 3000);
                        door.logined = false;
                        cache.go({
                            "tsy": "",
                            "un": "",
                            "uid": "",
                            "HURL":""
                        })
                    }
                    else{
                        tip.off('登出中...', 1);
                        tip.on(data.m, 0, 6000);
                    }
                }
            });
        }


    })
})